package com.example.travelcomponentdemo;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

public class SlidingForMap extends FragmentActivity
						implements OnTouchListener{
	
	LinearLayout layout_bg_menu, layout_map;
	RelativeLayout layout_menu;
	Button btn_menu;
	
	Animation fadeInAnim, fadeOutAnim, 
			translateInAnim, translateOutAnim;
	
	ScrollView scroll_menu;
	private int DELAY_TIME = 500;
	private boolean isToggle = true;
	
	private enum MenuState {
		HIDING,
		SHOWING, 
		TOUCHING
	}
	
	// Previous touch position
    float prevX = 0;
    float prevY = 0;
	
	// menu is hidden initially
    private MenuState currentMenuState = MenuState.HIDING;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sliding_menu_for_map);
		
		layout_bg_menu = (LinearLayout) findViewById(R.id.layout_bg_menu);
		layout_bg_menu.setOnTouchListener(this);
		
//		layout_bg_menu.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				toggleMenu();
//			}
//		});
		
		layout_menu = (RelativeLayout) findViewById(R.id.layout_menu);
		layout_menu.setOnTouchListener(this);
//		layout_map = (LinearLayout) findViewById(R.id.layout_map);
//		layout_map.setOnTouchListener(this);
		
		btn_menu = (Button) findViewById(R.id.btn_menu);
		fadeInAnim = new AlphaAnimation(0.0f, 0.8f);
		fadeInAnim.setDuration(DELAY_TIME);
		fadeInAnim.setFillAfter(true);
		
		fadeOutAnim = new AlphaAnimation(0.8f, 0.0f);
		fadeOutAnim.setDuration(DELAY_TIME);
		
		translateInAnim = AnimationUtils.loadAnimation(this, R.anim.left_to_right_in);
		translateInAnim.setDuration(DELAY_TIME);
		translateInAnim.setFillAfter(true);
		
		translateOutAnim = AnimationUtils.loadAnimation(this, R.anim.right_to_left_out);
		translateOutAnim.setDuration(DELAY_TIME);
		
		btn_menu.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				toggleMenu();
			}
		});
	}
	
	private void toggleMenu(){
		if(currentMenuState == MenuState.HIDING) {
			layout_bg_menu.setVisibility(View.VISIBLE);
			layout_bg_menu.startAnimation(fadeInAnim);
			
			layout_menu.setVisibility(View.VISIBLE);
			layout_menu.startAnimation(translateInAnim);
			
			currentMenuState = MenuState.SHOWING;
		} else {
			layout_bg_menu.startAnimation(fadeOutAnim);
			layout_menu.startAnimation(translateOutAnim);
			
			layout_menu.setVisibility(View.GONE);
//			layout_bg_menu.setVisibility(View.GONE);
			
			currentMenuState = MenuState.HIDING;
		}
	}
	
	@Override
    public void onBackPressed() {
        if (currentMenuState == MenuState.SHOWING) {
            toggleMenu();
        } else {
            super.onBackPressed();
        }
    }

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// getRawX returns X touch point corresponding to screen
        // getX sometimes returns screen X, sometimes returns content View X
        int curX = (int)event.getRawX();
        int curY = (int)event.getRawY();
        switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				prevX = curX;
				prevY = curY;
	            return true;
	        case MotionEvent.ACTION_MOVE:
	        	float x = event.getRawX();
	        	float y = event.getRawY();

	        	float diffX = x - prevX;
	        	float diffY = y - prevY;

	        	prevX = x;
	        	prevY = y;
	        	if(layout_menu.getLeft() >= 0 && diffX > 0) {
	        		return false;
	        	}

	        	if(layout_menu.getLeft() + diffX >= 0) {
	        		diffX = -layout_menu.getLeft();
	        	}
	        	
	        	if(Math.abs(diffX) < 10 
	        			&& currentMenuState != MenuState.TOUCHING
	        			&& v.getId() == R.id.scroll_menu) {
	        		isToggle = true;
	        		return true;
	        	} else 
	        		isToggle = false;
	        	
	        	currentMenuState = MenuState.TOUCHING;
	        	layout_menu.offsetLeftAndRight((int) diffX);

	        	float alpha = (float) ((layout_menu.getRight() * 0.8)/layout_menu.getWidth());
	        	if(Build.VERSION.SDK_INT < 11) {
	        		final AlphaAnimation animation = new AlphaAnimation(alpha, alpha);
	        		animation.setDuration(0);
	        		animation.setFillAfter(true);
	        		layout_bg_menu.startAnimation(animation);
	        	} else {
	        		layout_bg_menu.setAlpha(alpha);
	        	}
	        	return true;
	        case MotionEvent.ACTION_UP:
	        	if(!isToggle) {
	        		isToggle = true;
	        		startScrollIfNeeded();
	        	} else
	        		toggleMenu();
				break;
			default:
				break;
		}
        return false;
	}
	
	private void startScrollIfNeeded(){
		if(layout_menu.getRight() >= layout_menu.getWidth()/2) {
			layout_menu.offsetLeftAndRight((int) -layout_menu.getLeft());
			currentMenuState = MenuState.SHOWING;
		} else {
			toggleMenu();
		}
	}
}
